package com.dingup.provider.feign.server.classInfo;

import com.dingup.api.adapter.classInfo.ClassInfoAdapter;
import com.dingup.api.bean.query.classInfo.ClassInfoForPkBean;
import com.dingup.api.bean.response.SelectBean;
import com.dingup.api.bean.response.classInfo.ClassInfoResponseSelectBean;
import com.dingup.api.enums.ClassUserTypeEnum;
import com.dingup.api.model.classInfo.ClassInfo;
import com.dingup.provider.service.classInfo.ClassInfoService;
import com.dingup.provider.service.classInfo.ClassUserInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * Created by wancheng on 2018/9/21.
 */
@RestController()
@RequestMapping("/feign/class_info")
public class ClassInfoFeign {
    private static Logger logger = LoggerFactory.getLogger(ClassInfoFeign.class);
    @Autowired
    ClassUserInfoService classUserInfoService;
    @Autowired
    ClassInfoService classInfoService;

    @RequestMapping(value = "/get_studentIds_by_teacherId.do", method = RequestMethod.POST)
    public List<String> getClassStudentIds(String teacherId) {
        List<String> studentIds = classUserInfoService.getStudentIdsByTeacherId(teacherId, ClassUserTypeEnum.TEACHER);
        return studentIds;
    }

    @RequestMapping(value = "/get_class_info_select_bean.do", method = RequestMethod.POST)
    public List<ClassInfoResponseSelectBean> getClassInfoSelectBean() {
        List<ClassInfoResponseSelectBean> res = null;
        try {
            res = classUserInfoService.getClassInfo(null);
        } catch (Exception e) {
            logger.error("get_class_info_select_bean Exception={}", e.getMessage());
            e.printStackTrace();
        }
        return res;
    }

    @RequestMapping(value = "/get_class_info_by_id.do", method = RequestMethod.POST)
    public ClassInfoAdapter getClassInfoById(Integer classId) {
        ClassInfoAdapter res = null;
        try {
            res = classUserInfoService.getClassInfoById(classId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return res;
    }

    @RequestMapping(value = "/get_class_info_for_pk_screen.do", method = RequestMethod.POST)
    public List<ClassInfo> getClassInfoForPkScreen(@RequestBody ClassInfoForPkBean bean) {
        List<ClassInfo> res = classInfoService.getClassInfo(bean);
        return res;
    }

    @RequestMapping(value = "/get_simple_class_info_by_class_id.do", method = RequestMethod.POST)
    public ClassInfo getClassInfoByClassId(Integer classId) {
        ClassInfo res = classInfoService.getClassInfo(classId);
        return res;
    }

    @RequestMapping(value = "/get_all_class_info_select_bean.do", method = RequestMethod.POST)
    public List<SelectBean> getAllClassInfoSelectBean() {
        List<ClassInfo> list = classInfoService.getAllClassInfo();
        List<SelectBean> res = list.stream().map(classInfo -> new SelectBean(classInfo.getClassName(), String.valueOf(classInfo.getId()))).collect(Collectors.toList());
        return res;
    }

    @RequestMapping(value = "/get_class_info_for_add_homework.do", method = RequestMethod.POST)
    public List<Map<String, Object>> getClassInfoForAddHomework(@RequestParam("teacherId") String teacherId) {
        List<Map<String, Object>> res = classInfoService.getClassInfoGroupByTeacherSelf(teacherId);
        return res;
    }

    @RequestMapping(value = "/get_student_teacher_for_add_homework.do", method = RequestMethod.POST)
    public Map<String, Object> getStudentAndTeacherForAddHomework(@RequestParam("classId") Integer classId) {
        Map<String, Object> res = classUserInfoService.getUserGroupByClassSelf(classId);
        return res;
    }

    @RequestMapping(value = "/get_class_info_by_lesson_id.do", method = RequestMethod.POST)
    public List<Integer> getClassInfoByLessonIdAndUserId(@RequestParam("lessonId") Integer lessonId) {
        List<ClassInfo> res = classInfoService.getClassByLessonIdAndUserId(lessonId, null);
        List<Integer> classIds = res.parallelStream().map(ClassInfo::getId).collect(Collectors.toList());
        return classIds;
    }
}
